/*漫画名称盒子*/
let name_json = [];
let url_name;
let loca = location.href;
/*null 提示*/
let null_div = `<div class="articl-2-2-right" id="articl-2-2-right"><div style="width: 300px;height: 200px;margin: 0 auto; text-align: center;"><img src="/img/null_img.png" style="width: 100%;height: 100%;"><p style="color: #999;">没有你要找的内容试试其他的吧。。</p></div></div>`;

/*肚子*/
let article = $("article");
$(function () {
    /*加载漫画名*/
    searchData();
    if (loca.indexOf("?") != -1) {

        let vlaue = loca.substring(loca.indexOf("?"));
        if (vlaue.length > 7) {
            let input_value = decodeURI(vlaue.split("=")[1]);

            /*本地存储*/
            localStorage.setItem("mh-" + input_value, input_value);

            dataAdd(input_value);
        }

    }

    /*查看查询缓存*/
    article.html(localStorageData(''));

})


/*删除单个缓存*/
let rem = (str) => {
    localStorage.removeItem(str);
    article.html(localStorageData(''));
}


/*历史记录 拼接数据*/
let localStorageData = (str) => {
    if (str === 'emty') {
        /*删除全部*/
        localStorage.clear();
        article.html(localStorageData(''));
        return;
    }
    /*获取全部 存储*/
    let local_json = localStorage.valueOf();
    let html = '';

    if (local_json.length != 0) {

        html += `<div class="history">
                            <div class="history-1">
                                <span class="history-1-left">历史记录</span>
                                <i class="fa fa-trash-o" onclick="localStorageData('emty')"></i>
                            </div>
                    <div class="history-box">`;

        for (let key in local_json) {
            if (key.indexOf("-") != -1) {
                html += `<div>
                                  <i class="fa fa-plus"></i>
                                  <span onclick="dataAdd('${local_json[key]}')">${local_json[key]}</span>
                                  <i class="fa fa-times" onclick="rem('${key}')"></i>
                             </div>`
            }
        }
        html += `</div></div>`;
    }
    return html;
}


/*按键监听*/
$("#search-id").keyup(function () {
    let search = $("input[name='search']").val();

    if (search == null || search == '') {
        article.html(localStorageData(''));
        return;
    }

    if (event.keyCode == 13) {
        /*本地存储*/
        localStorage.setItem("mh-" + search, search);
        dataAdd(search)
        return;
    }

    let box = $("#search-datalist");
    let num = 0;
    box.empty();
    for (let i = 0, len = name_json.length; i < len; i++) {

        if (name_json[i].indexOf(search) != -1) {
            box.append(`<option value="${name_json[i]}"></option>`);
            num++;
        }

    }
    if (num === 0) {
        box.append(`<option value="没有找到你想要的..."></option>`);
    }

})

/*点击按钮 搜索漫画*/
$(".searchPageBtn").click(function () {
    let search = $("input[name='search']").val();
    if (search === '' || search === null || search === undefined) {
        return;
    }
    /*本地存储*/
    localStorage.setItem("mh-" + search, search);

    dataAdd(search);
});


/*查询 漫画名称*/
let searchData = () => {
    $.ajax({
        url: Search_url.prefix + "/" + Search_url.name_value,
        type: "POST",
        dataType: "json",
        success: function (row) {
            if (row.code === web_status.SUCCESS) {
                if (row.data != null || row.data != undefined) {
                    name_json = row.data;
                }
            } else {
                $.modal.alertError("发生错误");
            }
        }
    });
}

/*请求数据*/
let dataAdd = (name) => {
    $("input[name='search']").val(name);
    $.operate.request(Search_url.prefix + "/" + Search_url.name_data, {value: name}, function (row) {
        /*关闭遮罩层*/
        $.modal.closeLoading();
        if (row.code === web_status.SUCCESS) {

            let listdata = row.data.lists;

            let avatar = row.data.avatar;

            if (avatar != null && avatar != '' && avatar != undefined){
                if (avatar != 'no'){
                    $(".right-img div").css("display","none");

                    $(".right-img img")
                        .css("display","block")
                        .attr("src",avatar);
                }
            }

            if (listdata.length != 0) {
                let html = htmldata(listdata);
                article.html(html);
            } else {
                article.html(null_div);
            }

        } else {
            $.modal.alertError("发生错误");
        }
    });
}

/*数据拼装*/
let htmldata = (list) => {
    let html = '<div class="articl-2-2-right" id="articl-2-2-right">';
    for (let i = 0, len = list.length; i < len; i++) {
        let name = list[i].name;
        if (name.length > 5) {
            name = name.substring(0, 5) + "..";
        }
        let author = list[i].author;
        if (author.length > 5) {
            author = author.substring(0, 5) + "..";
        }
        let newZhang = list[i].newZhang === '0' ? '完结' : list[i].newZhang;

        let remark = list[i].remark;
        if (remark.length > 50) {
            remark = remark.substring(0, 50) + "...";
        }

        html += `<div class="articl-2-2-right-1">
                          <div class="articl-2-2-left-box">
                              <a href="/manhua/look/over/${list[i].id}" target="_blank"><img src="${$.haHa.decode(list[i].theCover)}" alt="${list[i].name}" onerror="$.common.errorimg(this)"></a>
                          </div>
                          <div class="articl-2-2-right-box">
                              <a href="/manhua/look/over/${list[i].id}">${name}</a>
                              <span>${list[i].typeName}</span>
                              <p>作者：${author}</p>
                              <p>更新至：<span class="red">${newZhang}</span></p>
                          </div>
                      </div>`;


    }
    html += '</div>'
    return html;
}
